package com.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;

import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.player.Player;
import com.world.World;

import entities.hero.Hero;

public class LoadScreen implements Screen {	 
	private TiledMap map;
	private World world;
	private OrthogonalTiledMapRenderer renderer; 
	private OrthographicCamera camera;
	private BitmapFont font;
	private Start game;
	
	private Player player;
	
	public LoadScreen(Start game, Hero hero, String port) {
		this.game = game;
		
		world = new World();
		map = world.getMap();
		
		renderer = new OrthogonalTiledMapRenderer(map);
		camera = new OrthographicCamera();
		
		font = new BitmapFont();
		
		player = new Player(hero, (TiledMapTileLayer) map.getLayers().get(0), port);
		player.getClient().send(hero);
	}
	
	@Override
	public void show() {

	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		renderer.setView(camera);
		renderer.render();
		
		font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
		renderer.getSpriteBatch().begin();
		font.draw(renderer.getSpriteBatch(), "Waiting for other player", 512 - 50, 384);
		renderer.getSpriteBatch().end();
		
		if (isReady()) {
			startGame();
        }
	}

	@Override
	public void resize(int width, int height) {
		camera.viewportHeight = height;
		camera.viewportWidth = width;
		 
		camera.setToOrtho(false, World.WIDTH, World.HEIGHT);
		camera.update();
	}
	
	@Override
	public void dispose() {
		font.dispose();
		map.dispose();
		renderer.dispose();
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
	
	public static Hero checkForPlayer() {
		return null;
	}

	@Override
	public void hide() {
		
	}
	
	public boolean isReady() {
		return player.getReady();
	}
	
	public void startGame() {
		 game.setScreen(new GameScreen(game, player));
         dispose();
	}
}
